package com.ktkq.action.checkin;



import java.io.File;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.ktkq.action.BaseAction;
import com.ktkq.service.checkin.CheckInService;
import com.ktkq.util.PageFinder;
import com.ktkq.util.Pageing;
import com.ktkq.util.StringUtil;
import com.ktkq.util.TimeUtil;
import com.ktkq.vo.Academy;
import com.ktkq.vo.CheckIn;
import com.ktkq.vo.Course;


@SuppressWarnings("serial")
@Controller
public class CheckInAction extends BaseAction {
	
	private Pageing pageing;
	
	private PageFinder<CheckIn> pageFinder;
	
	 private List<File> file;
	 private List<String> fileFileName;
	 private List<String> fileContentType;
  
	
	 @Resource(name="checkInService") 
	 CheckInService checkInService;
	
	 CheckIn checkIn;
	
	int checkInId;
      
	String CheckInList; //考勤状态数组
	
	 int[] deleteList;  //批量删除
		String resultView;    //结果视图        
	@Override
	public String execute() throws Exception {
		
		if(pageFinder==null){
			pageFinder=new PageFinder<CheckIn>();
		}
		return INPUT;
	}
	/**
	 * 查询分页信息
	 * @return
	 */
	public String findCheckInList(){
		System.out.println(pageing);
		if(pageing==null){
			pageing=new Pageing();
		}
		if(pageFinder==null){
			pageFinder=new PageFinder<CheckIn>();
		}
		int page=pageFinder.getPageNo();
		pageFinder.setPageSize(100);
		pageing.setPageSize(100);
		pageing.setStartOfPage(pageFinder.getStartOfPage());
		//分页信息
		pageFinder=checkInService.queryList(pageing);
		pageFinder.setPageNo(page);
		pageFinder.setPageSize(100);
		pageFinder.refresh();
		return SUCCESS;
	}
	/**
	 * 通过ID查询信息
	 * @return
	 */
	public String findCheckInById(){
		if(pageFinder==null){
			pageFinder=new PageFinder<CheckIn>();
		}
			pageFinder.setDataList(checkInService.queryListById(checkInId));

		return "edit";
	}
	/**
	 * 添加信息
	 * @return
	 */
	public String addCheckIn(){
		String[] CheckArray = CheckInList.split(",");
		System.out.println(CheckArray.length);
		if(CheckArray.length>=3){
			 String truancy = CheckArray[2];  //旷课
			for(String ck: truancy.split(";")){
				if(!ck.equals("")){
					CheckIn che = checkInService.queryListById(Integer.parseInt(ck.trim())).get(0);
					che.setTruancy(che.getTruancy()+1);
					che.setTime(new TimeUtil().getTime());
					checkInService.update(che);
				}
			}
		}
		if(CheckArray.length>=2){
				 String BeLate = CheckArray[1];  //迟到
				for(String ck: BeLate.split(";")){
					if(!ck.equals("")){
						
						CheckIn che = checkInService.queryListById(Integer.parseInt(ck.trim())).get(0);
						che.setBeLate(che.getBeLate()+1);
						che.setTime(new TimeUtil().getTime());
						checkInService.update(che);
					}
				}
			}
		if(CheckArray.length>=1){
		 String Vacate = CheckArray[0]; // 请假
			for(String ck: Vacate.split(";")){
				if(!ck.equals("")){
					CheckIn che = checkInService.queryListById(Integer.parseInt(ck.trim())).get(0);
					che.setVacate(che.getVacate()+1);
					che.setTime(new TimeUtil().getTime());
					checkInService.update(che);
				}
			}
		}
		return INPUT;
	}
	
	/**
	 * 修改信息
	 * @return
	 */
	public String updateCheckIn(){
		if(checkIn !=null){
			checkIn.setTime(new TimeUtil().getTime());
			checkInService.update(checkIn);
		}
		resultView = "/admin/checkin_findCheckInList.action?pageFinder.pageNo=1";
		return "update";
	}
	/**
	 * 删除信息
	 * @return
	 */
	public String deleteCheckIn(){
		if(deleteList!=null){
		      for(int id:deleteList){
		    	  CheckIn stu =  new CheckIn();
		    	  stu.setId(id);
		    	  checkInService.delete(stu);
		      }
			}else if(checkIn !=null){
			checkInService.delete(checkIn);
		}
		resultView = "/admin/checkin_findCheckInList.action?pageFinder.pageNo=1";
		return "update";
	}
	/**
	 *获取考勤json
	 */
	public void queryCheckIn(){
		if(pageing==null)
			pageing=new Pageing();
		
			pageFinder=new PageFinder<CheckIn>();
		
			pageFinder.setPageSize(100);
		pageing.setPageSize(100);
		pageing.setStartOfPage(pageFinder.getStartOfPage());
		pageFinder = checkInService.queryList(pageing);
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			Gson g  = new Gson();
	        String json = g.toJson(new Gson().toJson(pageFinder));
	        PrintWriter pw = response.getWriter();
	        pw.print(json);
	        pw.flush();
	        pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	
	public Pageing getPageing() {
		return pageing;
	}
	public void setPageing(Pageing pageing) {
		this.pageing = pageing;
	}
	
	 public PageFinder<CheckIn> getPageFinder() {
		return pageFinder;
	}
	public void setPageFinder(PageFinder<CheckIn> pageFinder) {
		this.pageFinder = pageFinder;
	}
	public CheckInService getCheckInService() {
		return checkInService;
	}
	public void setCheckInService(CheckInService checkInService) {
		this.checkInService = checkInService;
	}
	public CheckIn getCheckIn() {
		return checkIn;
	}
	public void setCheckIn(CheckIn checkIn) {
		this.checkIn = checkIn;
	}
	public int getCheckInId() {
		return checkInId;
	}
	public void setCheckInId(int checkInId) {
		this.checkInId = checkInId;
	}
	/**
     * 返回上传文件的保存位置
     * 
     * @return
     */
    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath("/studentFile"); 
    }

 
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public int[] getDeleteList() {
		return deleteList;
	}
	public void setDeleteList(int[] deleteList) {
		this.deleteList = deleteList;
	}
	public String getResultView() {
		return resultView;
	}
	public void setResultView(String resultView) {
		this.resultView = resultView;
	}
	public String getCheckInList() {
		return CheckInList;
	}
	public void setCheckInList(String checkInList) {
		CheckInList = checkInList;
	}
	

	

	

	

  
	

}
